﻿using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;
using System.Web.Http.Description;
using System.Web;
using System.Net.Http;
using System.Web.Http.Dispatcher;
using System.Web.Http.WebHost;
using HttpController.Override;

namespace WebApp
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            HttpMessageHandler handler = GlobalConfiguration.DefaultHandler;

            log4net.Config.XmlConfigurator.Configure();

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            WebApiFilterConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            StructureMapIocConfig();
        }

        private void StructureMapIocConfig()
        {
            var container = Bootstrap.StructureMap.Bootstrapper.ConfigureStructureMap();

            System.Web.Mvc.DependencyResolver.SetResolver(new Bootstrap.StructureMap.StructureMapResolver(container));

            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver =
                new Bootstrap.StructureMap.ApiStructureMapResolver(container);
        }

        protected void Application_EndRequest()
        {            
            //ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
        }
    }
}